var starClicked = false;
var id=parseInt(window.location.hash.split("#")[1]);
var rate = {
    "repairOrderId":id,
    "attitudeRank":"",
    "speedRank":"",
    "qualityRank":""
}
$(function() {
    $('.star').click(function() {
        $(this).children('.selected').addClass('is-animated');
        $(this).children('.selected').addClass('pulse');

        var target = this;
        setTimeout(function() {
            $(target).children('.selected').removeClass('is-animated');
            $(target).children('.selected').removeClass('pulse');
        }, 1000);
        starClicked = true;
    })

    $('.half').click(function() {
        if (starClicked == true) {
            setHalfStarState(this)
        }
        $(this).closest('.rating').find('.js-score').text($(this).data('value'));

        $(this).closest('.rating').data('vote', $(this).data('value'));
        var type = $(this).parents(".weui-cell").data("type")
        var score = $(this).data('value');
        rate[type] = score;

    })

    $('.full').click(function() {
        if (starClicked == true) {
            setFullStarState(this)
        }
        $(this).closest('.rating').find('.js-score').text($(this).data('value'));
        $(this).find('js-average').text(parseInt($(this).data('value')));
        $(this).closest('.rating').data('vote', $(this).data('value'));
        var type = $(this).parents(".weui-cell").data("type")
        var score = parseInt($(this).data('value'));
        rate[type] = score;
    })

    $('.half').hover(function() {
        if (starClicked == false) {
            setHalfStarState(this)
        }

    })

    $('.full').hover(function() {
        if (starClicked == false) {
            setFullStarState(this)
        }
    })

    
    console.log(id);
    $("#btnSub").on("click",function(){
        if(rate.attitudeRank&&rate.speedRank&&rate.qualityRank){
            // $.alert("评价信息不完整");
            var content =$(".weui-textarea").val();
            rate.content=content;
            $.ajax({
                url:"complaint_addComment.json",
                data:rate,
                success:function(data){
                    if(data.success){
                        $.alert("评论成功")
                    }
                }
            })
            $.alert("提交成功");
            console.log(rate)
        }
        else{
            $.alert("信息不完整");
        }
    })
})

//pre parent node addClass && next parent node removeClass
function updateStarState(target) {
    $(target).parent().prevAll().addClass('animate');
    $(target).parent().prevAll().children().addClass('star-colour');
    $(target).parent().nextAll().removeClass('animate');
    $(target).parent().nextAll().children().removeClass('star-colour');
}

function setHalfStarState(target) {
    $(target).addClass('star-colour');
    $(target).siblings('.full').removeClass('star-colour');
    updateStarState(target)
}

function setFullStarState(target) {
    $(target).addClass('star-colour');
    $(target).parent().addClass('animate');
    $(target).siblings('.half').addClass('star-colour');
    updateStarState(target)
}
